<?php
ini_set('max_execution_time', 900);
include "./include/om_config.inc";
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $smarty = new SmartyWWW();
  $fromDate = isset($_POST['fromDateYear'])? $_POST['fromDateYear']."-".strtoupper($_POST['fromDateMonth'])."-".$_POST['fromDateDay']:mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
  $toDate = isset($_POST['toDateYear'])? $_POST['toDateYear']."-".strtoupper($_POST['toDateMonth'])."-".$_POST['toDateDay']:mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
  $smarty->assign("fromDate",$fromDate);
  $smarty->assign("toDate",$toDate);
  
  $displayItemName =0;
  if(isset($_POST['itemId']) && $_POST['itemId'] == '0')
  {
  	$displayItemName = '1';
  }
  $smarty->assign("displayItemName",$displayItemName);
  /////For Display in table ////
         
	$weekDay=array();
	$expiryDateBc=array();
	$itemName=array();
	$bhavcopyDate=array();
	$openPrice=array();
	$highPrice=array();
	$lowPrice=array();
	$closePrice=array();
	$openInterest=array();
	$openInterestDisp=array();
	$displayCurrentRow=array();
	$lotSize=array();
	$openInterestTot=array();
	$openInterestInLots=array();
	$openInterestInLotsTot=array();
	$exchangeValue = array();
	$highestHighPrice = 0 ;
	$lowestLowPrice   = 99999;
	
	//selectitem:Start
	$itemQuery = "SELECT itemId,min 
	               FROM item";
	$itemQueryResult = mysql_query($itemQuery);
	$count=0;
	while($itemQueryRow = mysql_fetch_array($itemQueryResult))
	 $itemMinArray[$itemQueryRow['itemId']] = $itemQueryRow['min'];
	//selectitem:End
	
	$selectQuery = "SELECT exchange,contractCode,expiryDate from bhavcopyf_o ORDER BY bhavcopyDate,expiryDate";
	$resultQuery = mysql_query($selectQuery);
	$count=0;
	while($rowQuery = mysql_fetch_array($resultQuery))
	{
	  $contract[$count] = $rowQuery['contractCode'];
	  $expiry[$count] = $rowQuery['expiryDate'];
	  $exchange[$count] = $rowQuery['exchange'];
	  $count++;
	}
	$currentItemId =isset($_POST['itemId'])? $_POST['itemId'] :0;
	$expDate = isset($_POST['expiryDate'])? $_POST['expiryDate'] : $expiry[0]; 
	$currentExchange = isset($_POST['exchange'])? $_POST['exchange'] : $expiry[0]; 
	
	$displayCount = 0;  
	$displayQuery = "SELECT * FROM bhavcopyf_o WHERE bhavcopyDate >= '".$fromDate."' AND bhavcopyDate <= '".$toDate."'";
	if($currentItemId > '0')									
	{
    $displayQuery .= " AND contractCode='".$currentItemId."' ";
	
	  if($currentExchange !='BSE CASH' && $expDate != 0)
	  { 
	    $displayQuery .= " AND expiryDate='".$expDate."'"; 
	  }
	}
	$displayQuery .="  AND exchange='".$currentExchange."' ORDER BY bhavcopyDate,contractCode,expiryDate";
	$displayResult = mysql_query($displayQuery);
	$currentTotal  = 0;
	$currentTotalInLots  = 0;
	$displayCountCurrentRow = 0;
	while($displayRow = mysql_fetch_array($displayResult))
	{   
		$weekDay[$displayCount]     = date("l", mktime(0,0,0,substr($displayRow['bhavcopyDate'],5,2),substr($displayRow['bhavcopyDate'],8,2),substr($displayRow['bhavcopyDate'],0,4)));
		$bhavcopyDateYYYYMMDD[$displayCount]= $displayRow['bhavcopyDate'];
		$bhavcopyDate[$displayCount] = mysqlToDDMMYY($displayRow['bhavcopyDate']);
		$itemName[$displayCount] 		 = $displayRow['contractCode'];
		$expiryDateBc[$displayCount] = $displayRow['expiryDateBc'];
		$openPrice[$displayCount] 	 = $displayRow['openPrice'];
		$highPrice[$displayCount]  	 = $displayRow['highPrice'];
		$lowPrice[$displayCount] 		 = $displayRow['lowPrice'];
		$closePrice[$displayCount]   = $displayRow['closePrice'];
		$openInterest[$displayCount] = $displayRow['openInterest'];
		$currentItemMin = (isset($itemMinArray[$displayRow['contractCode']]) && $itemMinArray[$displayRow['contractCode']] != 0) ? $itemMinArray[$displayRow['contractCode']] : 1;
		$openInterestInLots[$displayCount]  			= $displayRow['openInterest'] / $currentItemMin;
		
		//To find the Highest Price and Lowest Price from data : Start
		if($highestHighPrice < $highPrice[$displayCount])
		  $highestHighPrice = $highPrice[$displayCount];
		  
		if($lowestLowPrice > $lowPrice[$displayCount] && $lowPrice[$displayCount] != 0)
		  $lowestLowPrice = $lowPrice[$displayCount];
		//To find the Highest Price and Lowest Price from data : End
		
		if($displayCount > 0 && $itemName[$displayCount - 1] == $displayRow['contractCode'] 
		         && $bhavcopyDateYYYYMMDD[$displayCount - 1] == $displayRow['bhavcopyDate'])
		{
		  $displayCurrentRow[$displayCount] = "Y";
		  $displayCountCurrentRow           = $displayCount;
		}
		else
		  $displayCurrentRow[$displayCount] = "N";
		  
		if($displayCount > 0 && $itemName[$displayCount - 1] == $itemName[$displayCount] 
		         && $bhavcopyDateYYYYMMDD[$displayCount - 1] == $displayRow['bhavcopyDate'])
		{
		  $openInterestDisp[$displayCount - 1]		= "N";//suppose, there are 3 rows, then 2nd row should be "N"
		  $openInterestDisp[$displayCount]  			= "Y";
		}
		else
		{
		  $currentTotal  = 0;
		  $currentTotalInLots  = 0;
		  $openInterestDisp[$displayCount]  			= "N";
		}
		$currentTotal  += $displayRow['openInterest'];
		$currentTotalInLots  += $displayRow['openInterest'] / $currentItemMin;
		$lotSize[$displayCount]             	= $currentItemMin;
		$openInterestTot[$displayCount]  			= $currentTotal;
		$openInterestInLotsTot[$displayCount]	= $currentTotalInLots;
		
		$displayCount++;
  }
  ////Request parameters, if passed : transfer to proper variable :End
  
  //Item records :Start
  $itemIdSelected = $currentItemId;
  $itemIdValues = array();
  $itemIdOptions = array();
  $itemCount = 0;
  $itemIdValues[0]  = '0';
  $itemIdOptions[0] = "All";
  $itemRecords = array();
  $itemCount++;
  $selectedExchange = isset($_POST['exchange'])? $_POST['exchange'] :'F_O';
  $smarty->assign("selectedExchange",$selectedExchange);
  
  $itemQuery = "SELECT DISTINCT(contractCode) 
                  FROM bhavcopyf_o 
                 WHERE exchange='".$selectedExchange."' 
                   AND contractCode NOT IN ('ALOKTEXT', 'ABB', 'MPHASIS', 'PEL', 'CNXINFRA', 'CNXIT', 'CNXPSE')
                 ORDER BY contractCode";
  $itemResult = mysql_query($itemQuery);
  while($itemRow = mysql_fetch_array($itemResult))
  {
    $itemIdValues[$itemCount]  = $itemRow['contractCode'];
    $itemIdOptions[$itemCount] = $itemRow['contractCode'];
    $itemCount++;
  }
  
  //Expiry records :Start
  if(isset($_POST['expiryDate']))
  {
    if($_POST['itemIdChanged']==1 || $currentItemId=="All")
      $expiryDateSelected = 0;
    else
      $expiryDateSelected = $_POST['expiryDate'];
  }
  else
    $expiryDateSelected = 0;
    
  $expiryDateValues = array();
  $expiryDateOptions = array();
  
  $i=0;
  $expiryDateValues[0]  = 0;
  $expiryDateOptions[0] = 'All';
  $i++;
    
  if($currentItemId!="All")
  {
    $expiryQuery = "SELECT DISTINCT(expiryDate),expiryDateBc FROM bhavcopyf_o 
                    WHERE contractCode='".$currentItemId."' ORDER BY expiryDate";
    $expiryResult = mysql_query($expiryQuery);
    while($expiryRow = mysql_fetch_array($expiryResult))
    {
      $expiryDateValues[$i]  = $expiryRow['expiryDate'];
      $expiryDateOptions[$i] = $expiryRow['expiryDateBc'];
      $i++;
    }
  }
  if(isset($_POST['avgLow']) && isset($_POST['avgHigh']))
  {	
    $avgHigh=$_POST['avgHigh'];
    $avgLow=$_POST['avgLow'];
  }
  
  $selectExchange="SELECT DISTINCT (exchange) FROM bhavcopyf_o";
  $resultExchange = mysql_query($selectExchange);    
  $j=0;
  while($rowExchange = mysql_fetch_array($resultExchange))
  {
  	$exchangeValue[$j] = $rowExchange['exchange'];
  	$j++;
  }
   
  //////End for display in table //////////
  $avgHigh = isset($avgHigh)? $avgHigh : "";

  $smarty->assign("weekDay",$weekDay);
  $smarty->assign("itemName",$itemName);
  $smarty->assign("expiryDateBc",$expiryDateBc);
  $smarty->assign("bhavcopyDate",$bhavcopyDate);
  $smarty->assign("openPrice",$openPrice);
  $smarty->assign("highPrice",$highPrice);
  $smarty->assign("lowPrice",$lowPrice);
  $smarty->assign("closePrice",$closePrice);
  $smarty->assign("openInterest",$openInterest);
  $smarty->assign("openInterestInLots",$openInterestInLots);
  $smarty->assign("openInterestDisp",$openInterestDisp);
  $smarty->assign("displayCurrentRow",$displayCurrentRow);
  $smarty->assign("lotSize",$lotSize);
  $smarty->assign("openInterestTot",$openInterestTot);
  $smarty->assign("openInterestInLotsTot",$openInterestInLotsTot);
  
  $smarty->assign("itemIdSelected",$itemIdSelected);
  $smarty->assign("itemIdValues",$itemIdValues);
  $smarty->assign("itemIdOptions",$itemIdOptions);
  $smarty->assign("expiryDateSelected",$expiryDateSelected);
  $smarty->assign("expiryDateValues",$expiryDateValues);
  $smarty->assign("expiryDateOptions",$expiryDateOptions);
  $smarty->assign("exchangeValue",$exchangeValue);
  $smarty->assign("highestHighPrice",$highestHighPrice);
  $smarty->assign("lowestLowPrice",$lowestLowPrice);

  $smarty->display('bhavcopyListF_O2.tpl');
}
?>